// stores/scrollStore.ts
import { defineStore } from 'pinia';
import store from '@/store'

type ScrollType = 'discountList' | 'buysList';

export const useScrollStore = defineStore('scroll', () => {
  // 存储滚动函数的 Map
  const scrollHandlers = new Map<ScrollType, (() => void)>();

  // 注册函数
  const registerScrollHandler = (type: ScrollType, handler: () => void) => {
    scrollHandlers.set(type, handler);
  };

  // 触发函数
  const triggerScroll = (type: ScrollType) => {
    const handler = scrollHandlers.get(type);
    handler?.(); // 安全调用
  };

  // 卸载时清理
  const clearScrollHandler = (type: ScrollType) => {
    scrollHandlers.delete(type);
  };

  return { registerScrollHandler, triggerScroll, clearScrollHandler };
});

/** 在 setup 外使用 */
export function useScrollStoreHook() {
  return useScrollStore(store)
}
